package com.ascentive.finallyfast.deepclean;

import android.database.Cursor;
import android.net.Uri;
import android.provider.CallLog;
import android.provider.CallLog.Calls;
import android.widget.Toast;
import com.ascentive.finallyfast.FinallyFastApplication;

public class CallIncomingHistory extends CallLogHistory {

		static int noOfCallsDeleted = 0;
	
		public CallIncomingHistory(FinallyFastApplication app, String name, boolean value, int img) {
			super(app, name, value, img);

		}

		@Override
		void doClean() {

			int noOfCallsDeleted = 0;
			int callDeleteStatus = 0;
			// Uri for CALL_LOG
			Uri allCalls = Uri.parse("content://call_log/calls");

			// Define projection
			String[] projection = new String[] { Calls.TYPE, Calls.NUMBER,
					Calls.DATE, Calls.CACHED_NAME, Calls.DURATION };

			// Query the CallLog for details
			// Details from the CallLog to be returned Calls are listed in order of
			// date of occurrence, with recent calls at the top
	        
			Cursor c = mApp.getContentResolver().query(allCalls, projection, "date <= (" + Utility.getDateBeforeNDays(mApp.getTargetDateDuration() ) +")", 
					null, Calls.DATE + " DESC");

			// Find past date depend on selected duration
			long pastDate = Utility.getDateBeforeNDays(mApp.getTargetDateDuration() );

			System.out.println("Number of total calls"+ c.getCount());
			if (c.moveToFirst()) {
				do { // For each result returned, the following occurs

//					String phNo =
//					c.getString(c.getColumnIndex(CallLog.Calls.NUMBER));
//					String length =
//					c.getString(c.getColumnIndex(CallLog.Calls.DURATION));
					String dateOfCall = c.getString(c
							.getColumnIndex(CallLog.Calls.DATE));
//					String typeOfCall = c.getString(c
//							.getColumnIndex(CallLog.Calls.TYPE));

					long lDateOfCall = (long) Double.parseDouble(dateOfCall);
	//
//					SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US);
//			    	String dateString = formatter.format(new Date(lDateOfCall));
			    	
			    	
					// System.out.println("Call Date");

					if (lDateOfCall < pastDate) {
//						System.out.println(typeOfCall);
//						if(typeOfCall.equals("1")) // Incoming Calls
//						{
//						System.out.println(phNo);
//						System.out.println(length);
//						System.out.println(dateString);
//						}

						// String queryString = "DATE <='" + pastDate + "'";
						String strNumber = "1"; // Incoming Calls 
		                String queryString= "TYPE='" + strNumber + "'";

						callDeleteStatus = mApp.getContentResolver().delete(allCalls, queryString,
							 	null);
						noOfCallsDeleted += callDeleteStatus;
					}
				} while (c.moveToNext());
			}

			if (noOfCallsDeleted >= 1) {
				String displayStr = new String();
				displayStr += noOfCallsDeleted;
				displayStr += " Numbers deleted";
				
				System.out.println("Number of calls deleted =="+ noOfCallsDeleted);
				
				Toast.makeText(mApp.getApplicationContext(), displayStr,
						Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(mApp.getApplicationContext(),
						"No such number in call logs", Toast.LENGTH_SHORT).show();
			}

			return;
		}
		
		@Override
		int numberOfFileCleaned() {
			// TODO Auto-generated method stub
			return noOfCallsDeleted;
		}
}